using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Data;
using Dimok.Data.Database;
namespace ImageToDisk
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button Start;
		public System.Windows.Forms.StatusBar stBar;
		private System.Windows.Forms.TextBox txtPath;
		private System.Windows.Forms.FolderBrowserDialog dlgFolder;
		private System.Windows.Forms.Button btnBrowse;
		private System.Windows.Forms.OpenFileDialog dlgFile;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnGetAddresses;
		public System.Windows.Forms.TextBox txtError;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MainForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Start = new System.Windows.Forms.Button();
			this.stBar = new System.Windows.Forms.StatusBar();
			this.txtPath = new System.Windows.Forms.TextBox();
			this.dlgFolder = new System.Windows.Forms.FolderBrowserDialog();
			this.btnBrowse = new System.Windows.Forms.Button();
			this.dlgFile = new System.Windows.Forms.OpenFileDialog();
			this.label1 = new System.Windows.Forms.Label();
			this.btnGetAddresses = new System.Windows.Forms.Button();
			this.txtError = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// Start
			// 
			this.Start.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.Start.Location = new System.Drawing.Point(449, 48);
			this.Start.Name = "Start";
			this.Start.Size = new System.Drawing.Size(72, 23);
			this.Start.TabIndex = 0;
			this.Start.Text = "Start";
			this.Start.Click += new System.EventHandler(this.Start_Click);
			// 
			// stBar
			// 
			this.stBar.Location = new System.Drawing.Point(0, 673);
			this.stBar.Name = "stBar";
			this.stBar.Size = new System.Drawing.Size(527, 19);
			this.stBar.TabIndex = 1;
			// 
			// txtPath
			// 
			this.txtPath.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtPath.Location = new System.Drawing.Point(8, 21);
			this.txtPath.Name = "txtPath";
			this.txtPath.Size = new System.Drawing.Size(433, 20);
			this.txtPath.TabIndex = 2;
			this.txtPath.Text = "";
			// 
			// dlgFolder
			// 
			this.dlgFolder.Description = "Select File With Applicants";
			// 
			// btnBrowse
			// 
			this.btnBrowse.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.btnBrowse.Font = new System.Drawing.Font("Microsoft Sans Serif", 8F);
			this.btnBrowse.Location = new System.Drawing.Point(449, 20);
			this.btnBrowse.Name = "btnBrowse";
			this.btnBrowse.Size = new System.Drawing.Size(72, 23);
			this.btnBrowse.TabIndex = 3;
			this.btnBrowse.Text = "Browse ...";
			this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
			// 
			// dlgFile
			// 
			this.dlgFile.DefaultExt = "xls";
			this.dlgFile.InitialDirectory = "C:\\\\";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(87, 20);
			this.label1.TabIndex = 4;
			this.label1.Text = "Text source file:";
			// 
			// btnGetAddresses
			// 
			this.btnGetAddresses.Location = new System.Drawing.Point(7, 62);
			this.btnGetAddresses.Name = "btnGetAddresses";
			this.btnGetAddresses.Size = new System.Drawing.Size(62, 20);
			this.btnGetAddresses.TabIndex = 5;
			this.btnGetAddresses.Text = "Addresses";
			this.btnGetAddresses.Visible = false;
			this.btnGetAddresses.Click += new System.EventHandler(this.btnGetAddresses_Click);
			// 
			// txtError
			// 
			this.txtError.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtError.Location = new System.Drawing.Point(0, 96);
			this.txtError.Multiline = true;
			this.txtError.Name = "txtError";
			this.txtError.Size = new System.Drawing.Size(528, 576);
			this.txtError.TabIndex = 6;
			this.txtError.Text = "";
			// 
			// MainForm
			// 
			this.AcceptButton = this.Start;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(527, 692);
			this.Controls.Add(this.txtError);
			this.Controls.Add(this.btnGetAddresses);
			this.Controls.Add(this.btnBrowse);
			this.Controls.Add(this.txtPath);
			this.Controls.Add(this.stBar);
			this.Controls.Add(this.Start);
			this.Controls.Add(this.label1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
			this.MinimumSize = new System.Drawing.Size(527, 128);
			this.Name = "MainForm";
			this.Text = "Image To Disk";
			this.Load += new System.EventHandler(this.MainForm_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		private void Start_Click(object sender, System.EventArgs e) {
			stBar.Text = "Start";
			Application.DoEvents();
			try{
				if( txtPath.Text == "" )
					throw new Exception("How 'bout a location for the text file with ApplicantIDs?");
				ImageTree.MakeImageTree(txtPath.Text,this);
			}catch(Exception exc){
				MessageBox.Show(exc.Message+Environment.NewLine+Environment.NewLine+exc.StackTrace);
			}
		}

		private void btnBrowse_Click(object sender, System.EventArgs e) {
			dlgFile.ShowDialog();
			txtPath.Text = dlgFile.FileName;
		}

		private string Conn{get{return ConfigurationSettings.AppSettings["conn"]+";Workstation ID=Admin.Dimok;Application Name=-1.";}}
		private void MainForm_Load(object sender, System.EventArgs e) {
//			string conn = ConfigurationSettings.AppSettings["conn"];
//			string connADO = ConfigurationSettings.AppSettings["connADO"];
//			DataSet dsAppl = SQLHelperEx.ExecuteDatasetEx(conn,"SELECT * FROM th_ADOTypes");
//			StreamWriter swDS = File.CreateText("D:\\Data\\ADOtoNet\\DS.htm");
//			foreach( DataColumn col in dsAppl.Tables[0].Columns )
//				swDS.WriteLine("{0}:{1}",col.ColumnName,col.DataType.ToString());
//			swDS.Close();
//			string adoRS = ConvertToRs.GetADORS(dsAppl,"AFC");
//			string adoRS1 = Dimok.Data.Database.SQLHelperEx.ADOToString(connADO,"SELECT * FROM th_ADOTypes");
//			StreamWriter sw = File.CreateText("D:\\Data\\ADOtoNet\\Net.htm");
//			sw.Write(adoRS);
//			sw.Close();
//			sw = File.CreateText("D:\\Data\\ADOtoNet\\ADO.htm");
//			sw.Write(adoRS1);
//			sw.Close();
		}

		private void btnGetAddresses_Click(object sender, System.EventArgs e) {
			string sql =  @"SELECT TOP 3000 t_CreditInfo.ApplicantID, t_CreditInfo.HumanReadable, t_Address.Street
											FROM  t_CreditInfo INNER JOIN
																		t_Address ON t_CreditInfo.ApplicantID = t_Address.ApplicantID
											WHERE (NOT (t_CreditInfo.HumanReadable IS NULL)) AND (t_Address.AddressByBureau IS NULL) AND (t_Address.[Index] = 0)
											ORDER BY t_CreditInfo.ApplicantID DESC";
			DataRowCollection drcCredit = SQLHelperEx.ExecuteDatasetEx(Conn,sql).Tables[0].Rows;
			Match address;
			foreach( DataRow rowCredit in drcCredit ){
				address = Dimok.Credit.Equifax.GetAddress(rowCredit["HumanReadable"]+"");
				if( address.Success )
					try{
						SQLHelperEx.ExecuteScalarEx(Conn,"UPDATE t_Address SET AddressByBureau = @Street WHERE ApplicantID=@ApplID AND [Index]=0",address.Value,rowCredit["ApplicantID"]);
					}catch{}
			}
		}
	}
}
